class InproductsController < ApplicationController
  # GET /inproducts
  # GET /inproducts.xml
  def index
    @inproducts = Inproduct.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @inproducts }
    end
  end

  # GET /inproducts/1
  # GET /inproducts/1.xml
  def show
    @inproduct = Inproduct.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @inproduct }
    end
  end

  # GET /inproducts/new
  # GET /inproducts/new.xml
  def new
    @inproduct = Inproduct.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @inproduct }
    end
  end

  # GET /inproducts/1/edit
  def edit
    @inproduct = Inproduct.find(params[:id])
  end

  # POST /inproducts
  # POST /inproducts.xml f_store_id
  def create
#    @inproduct = Inproduct.new(params[:inproduct])
      @inproducts = params[:inproducts].values
      @inproducts.each do |inproduct|
        if inproduct["total_amount"].to_i != 0 && inproduct["per_cost"].to_i > 0
          Inproduct.create(:f_store_id => params[:f_store_id].to_i,
                           :product_id => inproduct["id"].to_i,
                           :amount => inproduct["total_amount"].to_i,
                           :price => inproduct["per_cost"].to_i,
                           :order_num => inproduct["order_num"],
                           :total_price => inproduct["per_cost"].to_i * inproduct["total_amount"].to_i,
                           :description => inproduct["description"])
        product = Product.find(inproduct["id"].to_i)
        product.update_attribute("per_cost" ,inproduct["per_cost"].to_i )
        end
      end

#      flash[:notice] = 'Inproduct was successfully created.'
  redirect_to(:controller =>'products',:action =>'in_p_list',:f_store_id => params[:f_store_id].to_i)

  end
  
  
    def out_create
  #    @inproduct = Inproduct.new(params[:inproduct])
        @inproducts = params[:inproducts].values
        @inproducts.each do |inproduct|
          if inproduct["total_amount"].to_i != 0 #&& inproduct["per_cost"].to_i.abs > 0
            amount = -(inproduct["total_amount"].to_i.abs)
            logger.info "*" * 50
            logger.info amount.inspect
            Inproduct.create(:f_store_id => params[:f_store_id].to_i,
                             :product_id => inproduct["id"].to_i,
                             :amount => amount,
                             :price => inproduct["per_cost"].to_i,
                             :order_num => inproduct["order_num"],
                             :total_price => inproduct["per_cost"].to_i * amount,
                             :description => inproduct["description"],
                             :l_store_id => 4)
          product = Product.find(inproduct["id"].to_i)
          product.update_attribute("per_cost" ,inproduct["per_cost"].to_i )
          end
        end

  #      flash[:notice] = 'Inproduct was successfully created.'
    redirect_to(:controller =>'products',:action =>'out_p_list',:f_store_id => params[:f_store_id].to_i)

    end
  

  # PUT /inproducts/1
  # PUT /inproducts/1.xml
  def update
    @inproduct = Inproduct.find(params[:id])

    respond_to do |format|
      if @inproduct.update_attributes(params[:inproduct])
        flash[:notice] = 'Inproduct was successfully updated.'
        format.html { redirect_to("/products/in_p_list?f_store_id=#{params[:f_store_id]}") }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @inproduct.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /inproducts/1
  # DELETE /inproducts/1.xml
  def destroy
    @inproduct = Inproduct.find(params[:id])
    @inproduct.destroy

    respond_to do |format|
      format.html { redirect_to("/products/in_p_list?f_store_id=#{params[:f_store_id]}") }
      format.xml  { head :ok }
    end
  end
end
